package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据用户查询历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 订单更新操作
     * @param orders
     */
    void cancel(Orders orders);

    /**
     * 根据状态统计订单数量
     * @param toBeConfirmed
     * @param confirmed
     * @param deliveryInProgress
     * @return
     */
    @Select("SELECT " +
            "COUNT(CASE WHEN status = #{toBeConfirmed} THEN 1 END) AS toBeConfirmed, " +
            "COUNT(CASE WHEN status = #{confirmed} THEN 1 END) AS confirmed, " +
            "COUNT(CASE WHEN status = #{deliveryInProgress} THEN 1 END) AS deliveryInProgress FROM orders;")
    OrderStatisticsVO statistics(Integer toBeConfirmed, Integer confirmed, Integer deliveryInProgress);

    /**
     * 订单状态修改
     * @param ordersConfirmDTO
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void confirm(OrdersConfirmDTO ordersConfirmDTO);


    @Select("select * from orders where status = #{pendingPayment} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer pendingPayment, LocalDateTime time);

    @Select("select * from orders where number = #{orderNumber}")
    Orders getByOrderNumber(OrdersPaymentDTO ordersPaymentDTO);

    void payment(Orders orders);


}
